package service.impl;

import dao.ITicketDao;
import dao.impl.TicketDaoImpl;
import entity.Ticket;

import java.util.ArrayList;
import java.util.List;

public class TicketServiceImpl implements service.ITicketService {
    private final ITicketDao ticketDao = new TicketDaoImpl();
    private List<Ticket> tickets;

    public TicketServiceImpl() {
        tickets = ticketDao.load();
    }

    @Override
    public List<Ticket> findAll() {
        return tickets;
    }

    @Override
    public boolean add(Ticket ticket) {
        tickets.add(ticket);
        ticketDao.save(tickets);
        return true;
    }

    @Override
    public boolean delete(String id) {
        boolean result = tickets.removeIf(t -> t.getId().equals(id));
        if (result) {
            ticketDao.save(tickets);
        }
        return result;
    }

    @Override
    public void shutdown() {
        ticketDao.save(tickets);
    }

    @Override
    public Ticket findById(String id) {
        return tickets.stream().filter(t -> t.getId().equals(id)).findFirst().orElse(null);
    }
}